﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.UI.Models;
using SCMS.CoreBusinessLogic.ActionFilters;
using SCMS.CoreBusinessLogic.Web;
using SCMS.CoreBusinessLogic.Security;
using SCMS.CoreBusinessLogic.WB;
using SCMS.CoreBusinessLogic.TenderAnalysis;
using SCMS.CoreBusinessLogic._GoodsReceivedNote;
using SCMS.Model;
using SCMS.CoreBusinessLogic.CompletionCtificate;
using SCMS.CoreBusinessLogic._CountrySubOffice;

namespace SCMS.UI.Controllers
{
    [MyException]
    public class CompletionCertificateController : PortalBaseController
    {
        private ICompletionCertificateService ccService;
        private IWayBillService wbService;
        private ITAnalysisService taService;
        private ICountrySubOfficeService subOfficeService;
        public CompletionCertificateController(IPermissionService permissionService, IUserContext userContext, IWayBillService wbService,
            ITAnalysisService taService, IGoodsReceivedNoteService gRNService, ICompletionCertificateService ccService, ICountrySubOfficeService subOfficeService)
            : base(userContext, permissionService)
        {
            this.wbService = wbService;
            this.taService = taService;
            this.ccService = ccService;
            this.subOfficeService = subOfficeService;
        }
        //
        // GET: /CompletionCertificate/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LoadCC()
        {
            var model = new CompletionCtificate()
            {
                EntityCC = new Model.CompletionCertificate(),
                Staffs = new SelectList(taService.GetStaffs(countryProg.Id), "StaffID", "StaffName"),
                Offices = new SelectList(subOfficeService.GetCountrySubOffices(countryProg.Id), "Id", "Name"),
                POItems = new SelectList(ccService.GetGRNPurchaseOrders(countryProg.Id), "Id", "RefNumber")
            };
            model.EntityCC.RefNumber = "--New CC--";
            return View(model);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveCC(CompletionCtificate CCmodel)
        {
            CCmodel.EntityCC.CountryProgrammeId = countryProg.Id;
            CCmodel.EntityCC.PreparedBy = currentStaff.Id;
            CCmodel.EntityCC.PreparedOn = DateTime.Now;
            CCmodel.EntityCC.Id = Guid.NewGuid();
            ccService.SaveCompletionC(CCmodel.EntityCC);
            return ViewCC();
        }

        public ActionResult SubmitCC(Guid id)
        {
            Model.CompletionCertificate CC = ccService.GetCCById(id);
            CC.IsRejected = false;
            CC.IsSubmitted = true;
            CC.PreparedBy = currentStaff.Id;
            CC.PreparedOn = DateTime.Now;
            CC.RefNumber = ccService.GenerateUniquNumber(countryProg);
            ccService.SubmitCC(CC);
            return ViewCC();
        }

        public ActionResult ViewCC()
        {
            using (var db = new SCMSEntities())
            {
                List<Models.ViewCC> model = new List<ViewCC>();
                foreach (var item in ccService.GetCCNotes(countryProg.Id))
                {
                    var modelItem = new ViewCC()
                    {
                        EntityCC = item,
                        PreparedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == item.PreparedBy),
                        ComfirmedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == item.ConfirmedBy),
                        AprovedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == item.ApprovedBy)
                    };
                    model.Add(modelItem);
                }
                return View("ViewCC", model);
            }
        }

        public ActionResult ViewCCDetails(Guid id)
        {
            return View(CompletionCExtention.PrepareCC(id, ccService));
        }
    }
}
